<template>
  <div class="flex-app-continer">
    <vxe-modal
      show-zoom
      :zIndex='10'
      :showFooter='true'
      title='添加折扣'
      ref='modal1'
      :resize="true"
      width='1000'
      height='100vh'>
      <div class='form-con'>
        <div>已选 共{{selectRecords.length}}行 金额{{moneytype=='1' ? '（不含税）：' : '（含税）：'}}{{ amount }}</div>
        <vxe-form  :data="discountFormData" ref='searchForm' @submit="submitSearch" :rules="collectDataRules" size="mini" title-align="right" title-width="450" style="width: 100%">
          <vxe-form-item title="折扣方式：" field="discountType" :item-render="{}" >
            <template v-slot>
              <vxe-radio-group v-model="discountFormData.discountType" >
                <vxe-radio label="1" content="按金额折扣"></vxe-radio>
                <vxe-radio label="2" content="按比例折扣"></vxe-radio>
              </vxe-radio-group>
            </template>
          </vxe-form-item>
          <vxe-form-item title="折扣录入方式：" field="discountInputType" :item-render="{}">
            <template v-slot>
              <vxe-radio-group v-model="discountFormData.discountInputType" >
                <vxe-radio label="1" content="批量折扣录入"></vxe-radio>
                <vxe-radio label="2" content="逐条折扣录入"></vxe-radio>
              </vxe-radio-group>
            </template>
          </vxe-form-item>
          <vxe-form-item :title="moneytype=='1' ? '折扣金额（不含税）：' : '折扣金额（含税）：'" field="discountPrice" :item-render="{}" key='1' v-if='discountFormData.discountInputType==1 && discountFormData.discountType==1'>
            <template v-slot>
              <vxe-input  v-model="discountFormData.discountPrice" placeholder="请输入" type="number" min="0" :max="maxPrice" step="0.01" clearable></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="折扣比例（%）：" field="discountPercent" :item-render="{}" key='2' v-else-if='discountFormData.discountInputType==1 && discountFormData.discountType==2'>
            <template v-slot>
              <vxe-input  v-model="discountFormData.discountPercent" placeholder="请输入" type="number" min="0"  step="0.01" clearable></vxe-input>
            </template>
          </vxe-form-item>
        </vxe-form>
        <vxe-table header-align="center" border :auto-resize='autoSize' :sync-resize="autoSize"
                   ref="xTable1" :loading="loadingTable" :data="selectRecordsNew"
                   v-if='discountFormData.discountInputType==2 && discountFormData.discountType==1' key='aaa'>
          <vxe-column type="seq" title="序号" width="60" align="center"></vxe-column>
          <vxe-column field="xmmc" title="项目名称" ></vxe-column>
          <vxe-column field="bhsje" title="金额（不含税）" v-if="moneytype=='1'"></vxe-column>
          <vxe-column field="hsje" title="金额（含税）" v-else></vxe-column>
          <vxe-column field="discountPrice" :title="moneytype=='1' ? '*折扣金额（不含税）' : '*折扣金额（含税）'" type="html">
            <template v-slot="{ row }">
              <div class='input-box'>
                <vxe-input v-model="row.discountPrice" type="number" min="0" :max="moneytype=='1' ? row.bhsje : row.hsje" step="0.01" clearable ></vxe-input>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
        <vxe-table header-align="center" border :auto-resize='autoSize' :sync-resize="autoSize"
                   ref="xTable2" :loading="loadingTable" :data="selectRecordsNew"
                   v-if='discountFormData.discountInputType==2 && discountFormData.discountType==2' key='bbb'>
          <vxe-column type="seq" title="序号" width="60" align="center"></vxe-column>
          <vxe-column field="xmmc" title="项目名称" ></vxe-column>
          <vxe-column field="bhsje" title="金额（不含税）" v-if="moneytype=='1'"></vxe-column>
          <vxe-column field="hsje" title="金额（含税）" v-else></vxe-column>
          <vxe-column field="discountPercent" title="*折扣比例（%）" type="html">
            <template v-slot="{ row }">
              <div class='input-box'>
                <vxe-input v-model="row.discountPercent" type="number" min="0" step="0.01" clearable ></vxe-input>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <template v-slot:footer>
        <vxe-button @click="closeSearch" status="info">取消</vxe-button>
        <vxe-button @click="submitSearch" status="primary">保存</vxe-button>
      </template>
    </vxe-modal>
  </div>
</template>
<script>
import { mixin } from '@/utils/mixin.js'
// import { postAction} from '@/api/manage';
export default {
  name: 'discountComp',
  mixins: [mixin],
  props: {
    moneytype: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      amount:0,
      selectRecordsNew:[],
      selectRecords:[],
      maxPrice:0,
      discountFormData: {
        discountType: '1',
        discountInputType: '1',
        discountPrice:'',
        discountPercent:''
      },
      collectDataRules: {
        discountPrice: [
          {required: true, message: '请输入'},
          {
            validator ({ itemValue }) {
              // 自定义校验
              if (Number(itemValue) <= 0) {
                return new Error('必须大于0')
              }
            }
          }
        ],
        discountPercent: [
          {required: true, message: '请输入'},
          {
            validator ({ itemValue }) {
              // 自定义校验
              if (Number(itemValue) <= 0) {
                return new Error('必须大于0')
              }
            }
          }
        ]
      },
      autoSize:true,
      loadingTable: false // 刷新
    }
  },
  components: {
  },
  // mixins: [mixin],
  created() {
  },
  mounted() {

  },
  methods: {
    async submitSearch(){
      let errMap = null;
      errMap = await this.$refs.searchForm.validate()
      if (errMap) {
        return
      }
      let selectRecords = this.selectRecordsNew
      selectRecords.map(item => {
        if(this.discountFormData.discountInputType==1 && this.discountFormData.discountType==1){
          let n1 = Number(this.discountFormData.discountPrice)
          item.discountPrice = 0-n1
        }else if(this.discountFormData.discountInputType==1 && this.discountFormData.discountType==2){
          let price = this.moneytype=='1' ? item.bhsje : item.hsje
          let n1 = Number(price)
          let n2 = Number(this.discountFormData.discountPercent)
          let p = (n1 * n2 / 100).toFixed(2)
          item.discountPrice = 0-p
        }else if(this.discountFormData.discountInputType==2 && this.discountFormData.discountType==2){
          let price = this.moneytype=='1' ? item.bhsje : item.hsje
          let n1 = Number(price)
          let n2 = Number(item.discountPercent)
          let p = (n1 * n2 / 100).toFixed(2)
          item.discountPrice = 0-p
        }else{
          let n1 = Number(item.discountPrice)
          item.discountPrice = 0 - n1
        }
        if(this.moneytype=='1'){ //不含税
          let tmp = item.discountPrice
          item.bhsjenew = tmp
          item.hsjenew = (tmp*(1+item.slv)).toFixed(2)
        }else{
          let tmp = item.discountPrice
          item.hsjenew = tmp
          item.bhsjenew = (tmp/(1+item.slv)).toFixed(2)
        }
      })
      this.$emit('discounted',{discountArr:selectRecords});
      this.$refs.modal1.close()
    },
    closeSearch(){
      this.$refs.modal1.close()
    },
    show(selectRecords) {
      this.$refs.modal1.open()
      this.discountFormData.discountPercent = 0
      this.discountFormData.discountPrice = 0
      let amount = 0
      let arr = []
      this.selectRecords = selectRecords;
      if(this.moneytype=='1') { //不含税
        this.maxPrice = Number(selectRecords[0].bhsje)
      }else{
        this.maxPrice = Number(selectRecords[0].hsje)
      }
      selectRecords.map(item => {
        let tmp;
        if(this.moneytype=='1'){ //不含税
          tmp = Number(item.bhsje)
        }else{
          tmp = Number(item.hsje)
        }
        amount = amount + tmp
        if(tmp < this.maxPrice){
          this.maxPrice = tmp
        }
        let obj = {}
        obj.xmmc = item.xmmc
        obj.bhsje = Number(item.bhsje)
        obj.hsje = Number(item.hsje)
        obj.discountPrice = ''
        obj.discountPercent = ''
        obj.slv = Number(item.slv)
        arr.push(obj)
      })
      this.selectRecordsNew = arr
      this.amount = amount.toFixed(2)
    }
  },
}
</script>
<style lang="scss">
.op-area{position: absolute;left: 0;top:0;}
.expand-wrapper {
  padding: 20px;
}
.form-con{width: 900px; margin:0 auto;}
.vxe-form--item-title{max-width: 500px;}
.radio-area{width: 210px; margin:0 auto;position: relative;left:10px;padding-bottom:5px;}
.radio-tit{margin-right:10px;}
.box-tit{margin-bottom:10px;}
.my-search.vxe-input {
  height: 34px;
  width: 300px;
}
</style>